<?php
declare (strict_types = 1);

namespace app\controller;


use think\db\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;
use think\Response;
use think\facade\View;
use think\facade\Validate;
use think\validate\ValidateRule as Rule;
use app\validate\Login as LoginValidate;


//use think\validate\ValidateRule as Rule;

class Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        //return "{name:'Apple'}" ;
        //return View::fetch('login');
        //return View::fetch('signin');
        //Response::create([],'欢迎登录',200);
        response([], 200);
        return View::fetch('login');

    }

    /**
     * 添加用户信息
     *
     * @return \think\Response
     */
    public function insert(Request $request)
    {
        //return 'insert...';
        $username = input('username');
        $password = input('password');

        //验证规则
        $rules1 = [
            'uid|用户名' => 'require|unique:teacheruser|max:25',
            'pwd|密码' => 'require|min:6|max:25',

        ];

        $rules2 = [
            'stu_no|用户名' => 'require|unique:stuuser|max:25',
            'pwd|密码' => 'require|min:6|max:25',

        ];

        $msg1 = [
            'uid.require' => '名称必须',
            'uid.max' => '名称最多不能超过25个字符',
            'uid.unique' => '名称必须唯一',
            'pwd.require' => '密码必须',
            'pwd.min' => '密码最少超过6个字符',
            'pwd.max' => '密码最多不能超过25个字符',
        ];
        $msg2 = [
            'stu_no.require' => '名称必须',
            'stu_no.max' => '名称最多不能超过25个字符',
            'stu_no.unique' => '名称必须唯一',
            'pwd.require' => '密码必须',
            'pwd.min' => '密码最少超过6个字符',
            'pwd.max' => '密码最多不能超过25个字符',
        ];

        $data1 = [
            'uid' => $username,
            'pwd' => $password,
        ];

        $data2 = [
            'stu_no' => $username,
            'pwd' => $password,
        ];
        /*验证规则,用户名长度小于12位学号，即为教师用户*/
        $is_teacher = false;
        if (strlen($username) < 12) {
            $is_teacher = true;
            $validate = validate($rules1, $msg1, true, true);
            if (!$validate->check($data1)) {
                //dump($validate->getError());
                return Response::create(['code' => 400, 'msg' => $validate->getError(), 'data' => []], 'json', 400);
            } else {
                try {
                    /*添加教师用户*/
                    $result = Db::table('tb_teacheruser')->insert(['uid' => $username, 'pwd' => $password]);
                    return Response::create(['code' => 200, 'msg' => '添加用户成功', 'data' => []], 'json', 200);
                } catch (DbException $e) {
                    return Response::create(['code' => 400, 'msg' => $e->getMessage(), 'data' => []], 'json', 400);
                }
            }

        }
        $validate = validate($rules2, $msg2, true, true);
        if (!$validate->check($data2)) {
            //dump($validate->getError());
            return Response::create(['code' => 400, 'msg' => $validate->getError(), 'data' => []], 'json', 400);
        } else {
            try {
                /*添加学生用户*/
                $result = Db::table('tb_stuuser')->insert(['stu_no' => $username, 'pwd' => $password]);
                return Response::create(['code' => 200, 'msg' => '添加用户信息成功', 'data' => []], 'json', 200);
            } catch (DbException $e) {
                return Response::create(['code' => 400, 'msg' => $e->getMessage(), 'data' => []], 'json', 400);
            }
        }
    }

    /**
     * 用户密码重置
     *
     * @return \think\Response
     */
    public function registpwd(Request $request)
    {
        //return 'insert...';
        $username = input('username');
        $password = input('password');

        //验证规则
        $rules1 = [
            'uid|用户名' => 'require|max:25',
            'pwd|密码' => 'require|min:6|max:25',

        ];

        $rules2 = [
            'stu_no|用户名' => 'require|max:25',
            'pwd|密码' => 'require|min:6|max:25',

        ];

        $msg1 = [
            'uid.require' => '名称必须',
            'uid.max' => '名称最多不能超过25个字符',
            'uid.unique' => '名称必须唯一',
            'pwd.require' => '密码必须',
            'pwd.min' => '密码最少超过6个字符',
            'pwd.max' => '密码最多不能超过25个字符',
        ];
        $msg2 = [
            'stu_no.require' => '名称必须',
            'stu_no.max' => '名称最多不能超过25个字符',
            'stu_no.unique' => '名称必须唯一',
            'pwd.require' => '密码必须',
            'pwd.min' => '密码最少超过6个字符',
            'pwd.max' => '密码最多不能超过25个字符',
        ];

        $data1 = [
            'uid' => $username,
            'pwd' => $password,
        ];

        $data2 = [
            'stu_no' => $username,
            'pwd' => $password,
        ];
        /*验证规则,用户名长度小于12位学号，即为教师用户*/
        $is_teacher = false;
        if(strlen($username) < 12){
            $is_teacher = true;
            $id = Db::table('tb_teacheruser')->where('uid',$username)->select('id');
            if(empty($id)){
                return Response::create(['code' => 400, 'msg' => '无法找到该用户,请重新输入用户名', 'data' => []], 'json', 400);
            }
        }

        if (strlen($username) < 12) {
            //$is_teacher = true;
            $validate = validate($rules1, $msg1, true, true);
            if (!$validate->check($data1)) {
                //dump($validate->getError());
                return Response::create(['code' => 400, 'msg' => $validate->getError(), 'data' => []], 'json', 400);
            } else {
                /*教师用户密码重置*/
                $id = Db::table('tb_teacheruser')->where('uid', $username)->column('id');
                try {
                    $result = Db::table('tb_teacheruser')
                        ->where('id', $id[0])
                        ->update(['pwd' => $password]);
                    //var_dump($result);
                    if (!empty($result)) {
                        return Response::create(['code' => 200, 'msg' => '密码重置成功', 'data' => []], 'json', 200);
                    } else {
                        return Response::create(['code' => 400, 'msg' => '密码重置失败', 'data' => []], 'json', 400);
                    }
                } catch (DbException $e) {
                    return Response::create(['code' => 400, 'msg' => $e->getMessage(), 'data' => []], 'json', 400);
                }
            }
        }
            $validate = validate($rules2, $msg2, true, true);
            if (!$validate->check($data2)) {
                //dump($validate->getError());
                return Response::create(['code' => 400, 'msg' => $validate->getError(), 'data' => []], 'json', 400);
            } else {
                /*学生用户密码重置*/
                $id = Db::table('tb_stuuser')->where('stu_no', $username)->column('id');
                try {
                    $result = Db::table('tb_stuuser')
                        ->where('id', $id[0])
                        ->update(['pwd' => $password]);
                    //var_dump($result);
                    if (!empty($result)) {
                        return Response::create(['code' => 200, 'msg' => '密码重置成功', 'data' => []], 'json', 200);
                    } else {
                        return Response::create(['code' => 400, 'msg' => '密码重置失败', 'data' => []], 'json', 400);
                    }
                }catch (DbException $e){
                    return Response::create(['code' => 400, 'msg' => $e->getMessage(), 'data' => []], 'json', 400);
                }
            }

    }

    /*用户注册*/
    public function signin(Request $request){
        echo 'signin..';
        return View::fetch('signin');
        //$strUrl = $_GET['strUrl'];
        //echo "document.location.href='".$str."'";
        //return "signin..";
    }

    /*用户密码重置*/
    public function forgetpwd(Request $request){
        echo 'forgetpwd..';
        return View::fetch('forgetpwd');
        //$strUrl = $_GET['strUrl'];
        //echo "document.location.href='".$str."'";
        //return "signin..";
    }
    /*教师用户登录*/
    public function teacher(Request $request){
        echo 'teacher..';
        return View::fetch('teacher');
        //$strUrl = $_GET['strUrl'];
        //echo "document.location.href='".$str."'";
        //return "signin..";
    }

    /*学生用户登录*/
    public function user(Request $request){
        echo 'user..';
        return View::fetch('user');
        //$strUrl = $_GET['strUrl'];
        //echo "document.location.href='".$str."'";
        //return "signin..";
    }

    /**
     * 用户登录页面
     *
     * @return \think\Response
     */
    public function login2(Request $request)
    {
        //echo "Login2...";
        $username  = input('username');
        $password = input('password');

        /*$result = [
            //状态码
            'code' => 400,
            //自定义消息
            'msg' => '用户名或密码不能为空',
            //数据返回
            'data' =>[]
        ];*/

        //将数据返回或指定格式api接口，默认json
        //return Response::create($result,$type);
        if(empty($username) && empty($password)){
/*            echo "<?php<script type='text/javascript'>alert('用户名或密码不能为空'）</script>?>";*/
            return Response::create(['code' => 400,'msg' => '用户名或密码不能为空', 'data' => []],'json',400);
        }elseif (empty($password)){
/*            echo "<script type='text/javascript'>alert('用户名或密码不能为空');</script>?>";*/
            return Response::create(['code' => 400,'msg' => '用户名或密码不能为空', 'data' => []],'json',400);
        }

        $data= $request->param();
        //验证返回
        /*try {
            validate(LoginValidate::class)->check($data);
            return Response::create(['code' => 200,'msg' => '用户名及密码符合规则', 'data' => $data],'json',200);
        }catch(ValidateException $e){
            //错误返回
            return Response::create(['code' => 400,'msg' => $e->getMessage(), 'data' => []],'json',400);
        }*/

        //获取数据库中的用户信息
        //$stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->find('pwd');
        $stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->column('pwd');
        $teacher1 = Db::table('tb_teacheruser')->where('uid',$username)->column('pwd');

        $is_teacher = false;
        $is_student = false;
        if(empty($stu1)){
            $is_teacher = true;
        }
        if(empty($teacher1)){
            $is_student = true;
        }
        if($is_teacher && $is_student){
            return Response::create(['code' => 400,'msg' => '请输入重新输入用户名和密码', 'data' => []],'json',400);
        }
        /*echo json_encode($stu1);
        echo json_encode($teacher1);*/
        //return Response::create(['code' => 400,'msg' => '用户名或者密码不正确', 'data' => []],'json',400);
        if($is_teacher){
            return Response::create(['code' => 200,'msg' => '用户名及密码正确,欢迎教师登录', 'data' => ['teacher',$username]],'json',200);
            //return View::fetch('teacher');
        }
        if($is_student){
            return Response::create(['code' => 200,'msg' => '用户名及密码正确,欢迎学生登录', 'data' => ['student',$username]],'json',200);
            //return View::fetch('user');
        }
    }
    public function login(Request $request)
    {
        $username  = input('username');
        $password = input('password');
        //$qrpassword = input('qrpassword');

        //验证规则
        // $rules = [
        //     'username'  => 'require|max:25',
        //     'password' => 'require|min:6|max:25',
        // ];

        /*$msg = [
            'username.require' => '名称必须',
            'username.max'     => '名称最多不能超过25个字符',
            'password.require' => '密码必须',
            'password.min'     => '密码最少超过6个字符',
            'password.max'     => '密码最多不能超过25个字符',
            // 'age.number'   => '年龄必须是数字',
            // 'age.between'  => '年龄只能在1-120之间',
            // 'email'        => '邮箱格式错误',
        ];*/

        /*$field = [
            'username'  => '名称',
            'password'   => '密码',
        ];*/

        /*$data = [
            'username'  => $username,
            'password'   => $password,
        ];

        $validate = Validate::rule(
            [
                'username'  => Rule::isRequire()->max(25),
                'password' => Rule::isRequire()->min(6)->max(25),
            ]);
        //$result   = $validate->batch()->check($data);
        if(!$validate->check($data)){
            dump($validate->getError());
            return ":code:304";
        }*/

        /*//验证规则
        $data = [
            'username'  => $username,
            'password' => $password,
            //'qrpassword' => $qrpassword,
        ];*/

        // if (!$validate->check($data)) {
        //     dump($validate->getError());
        //     echo $username;
        //     return ;
        // }

        // echo $username;
        // echo ":";
        // echo $username,$password,$qrpassword;


        //获取数据库中的用户信息
        //$stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->find('pwd');
        $stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->column('pwd');
        $teacher1 = Db::table('tb_teacheruser')->where('uid',$username)->column('pwd');
        
        $is_teacher = false;
        $is_student = false;

        //dump($stu1);
        //dump($teacher1);

        if(empty($stu1) && empty($teacher1)){
            //return "用户名不正确，口令不正确";
            //return $this->create([],'用户名不正确，口令不正确',400);
        }    

        if(!$stu1){
            $is_teacher = true;
            if($password == $teacher1['0']){
                //return "教师用户名正确,欢迎登录";
                echo "教师用户名正确,欢迎登录";
                return View::fetch('user');
            }else{
                return "教师口令不正确，请重新输入！";
            }
        }

        if(!$teacher1){
            $is_student = true;
            if($password == $stu1['0']){
                //return "学生用户名正确,欢迎登录";
                echo "学生用户名正确,欢迎登录";
                return View::fetch('user');
            }else{
                return "学生口令不正确，请重新输入！";
            }
        }
        //dump($request);
        //dump($request);
        //return "login.." ;
        //return View::fetch('login');
        
    }

    
    /**
     * 用户登录成功
     *
     * @return \think\Response
     */
    
   /* public function user(Request $request){
        echo "用户登录成功！";
        return View::fetch('user');
    }*/

    public function login_enable(Request $request)
    {
        $username  = input('username');
        $password = input('password');
        $qrpassword = input('qrpassword');

        $validate = \think\facade\Validate::rule('age', 'number|between:1,120')
        ->rule([
            'username'  => 'require|max:25',
            'password' => 'require|min:6|max:25',
            'qrpassword' => 'require|min:6|max:25',
        ]);
        

        //验证规则
        $data = [
            'username'  => $username,
            'password' => $password,
            'qrpassword' => $qrpassword,
        ];

        // if (!$validate->check($data)) {
        //     dump($validate->getError());
        //     echo $username;
        //     return ;
        // }

        // echo $username;
        // echo ":";
        // echo $username,$password,$qrpassword;


        //获取数据库中的用户信息
        $stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->find();
        $teacher1 = Db::table('tb_teacheruser')->where('uid',$username)->find();
        $is_teacher = false;
        $is_student = false;

        //dump($stu1);
        //dump($teacher1);
        if($stu1 != null){
            $is_student = true;
            return "学生用户名正确,欢迎登录";
        }else if($teacher1 != null){
            $is_teacher = true;
            return "教师用户名正确,欢迎登录";
        }else{
            return "用户名不正确,请重新输入!";
        }

        
        //dump($request);
        //dump($request);
        //return "login.." ;
        //return View::fetch('login');
        
    }

    public function loginold(Request $request)
    {
        $username  = input('username');
        $password = input('password');
        $qrpassword = input('qrpassword');

        $validate = \think\facade\Validate::rule('age', 'number|between:1,120')
        ->rule([
            'username'  => 'require|max:25',
            'password' => 'require|min:6|max:25',
            'qrpassword' => 'require|min:6|max:25',
        ]);
        

        //验证规则
        $data = [
            'username'  => $username,
            'password' => $password,
            'qrpassword' => $qrpassword,
        ];

        // if (!$validate->check($data)) {
        //     dump($validate->getError());
        //     echo $username;
        //     return ;
        // }
        echo $username;
        echo ":";
        echo $username,$password,$qrpassword;


        //获取数据库中的用户信息
        $stu1 = Db::table('tb_stuuser')->where('stu_no',$username)->find();
        $teacher1 = Db::table('tb_teacheruser')->where('uid',$username)->find();
        //dump($stu1);
        //dump($teacher1);

        if($teacher1 != null && $stu1 == null){
            //$teacher1 = Db::table('tb_teacheruser')->where('uid',$username)->find();
            if($username == $teacher1['uid']){
                return "教师用户名正确,欢迎登录";
            }
        }else if($stu1 == null && $teacher1 == null){
            dump($stu1);
            dump($teacher1);
            return "用户名不正确,请重新输入";
        }
        if($stu1 != null && $teacher1 == null){
            if($username == $stu1['stu_no']){
                return "学生用户名正确,欢迎登录";
            }
        }else if($stu1 == null && $teacher1 == null){
            return "学生用户名不正确,请重新输入";
        }

        // if($stu1 != null && $stu1['stu_no'] != $username){
        //     if($teacher1 != $teache1){
        //         return "用户名不正确";
        //     }else if($username == $teachername1){
        //         echo "教师用户名正确";
        //         return "欢迎登录";
        //     }
        // }else if($username == $stuname1){
        //     echo "学生用户名正确";
        //         return "欢迎登录";
        // }
        
        //dump($request);
        //dump($request);
        //return "login.." ;
        //return View::fetch('login');
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
